package com.project.school_flate.serviceimpl.flow;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.project.school_flate.dto.flow.FlowBusinessDto;
import com.project.school_flate.dto.shop.ShopSaveUserDto;
import com.project.school_flate.entity.flow.FlowBusiness;
import com.project.school_flate.entity.flow.table.FlowBusinessTableDef;
import com.project.school_flate.mapper.flow.FlowBusinessMapper;
import com.project.school_flate.service.flow.FlowBusinessService;
import com.project.school_flate.util.PoToDTO;
import com.project.school_flate.util.Result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service(value = "flowBusinessServiceImpl")
public class FlowBusinessServiceImpl implements FlowBusinessService {

    @Autowired
    private FlowBusinessMapper oFlowBusinessMapper;

    /**
     * 获取业务流水
     * @param oFlowBusinessDto
     * @return
     * @throws Exception
     */
    @Override
    public Result getFlowBusiness(FlowBusinessDto oFlowBusinessDto) throws Exception {
        List<FlowBusiness> oFlowBusinessList = new ArrayList<>();
        long total = 0;
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.orderBy("create_time desc");
        //是否分页
        if(oFlowBusinessDto.getPage() != null && oFlowBusinessDto.getLimit() != null){
            Page<FlowBusiness> FlowBusinessPage = oFlowBusinessMapper.paginateWithRelations(oFlowBusinessDto.getPage(),oFlowBusinessDto.getLimit(),queryWrapper);
            oFlowBusinessList = FlowBusinessPage.getRecords();
            total = FlowBusinessPage.getTotalRow();
        }else{
            oFlowBusinessList = oFlowBusinessMapper.selectListWithRelationsByQuery(queryWrapper);
            total = oFlowBusinessList.size();
        }
        //PoToDto
        List<FlowBusinessDto> oFlowBusinessDtoList = (List<FlowBusinessDto>) PoToDTO.poToDtoList(oFlowBusinessList,new FlowBusinessDto());
        return Result.ok(oFlowBusinessDtoList,total);
    }

    /**
     * 获取用户在店铺储蓄消费
     * @param oFlowBusinessDto
     * @return
     * @throws Exception
     */
    @Override
    public Result getFlowMoneyConsumptionToShopSaveUser(FlowBusinessDto oFlowBusinessDto) throws Exception {
        List<FlowBusiness> oFlowBusinessList = new ArrayList<>();
        long total = 0;
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.where(FlowBusinessTableDef.FLOW_BUSINESS.SHOP_ID.eq(oFlowBusinessDto.getShopId()));
        queryWrapper.where(FlowBusinessTableDef.FLOW_BUSINESS.USER_ID.eq(oFlowBusinessDto.getUserId()));
        queryWrapper.where(FlowBusinessTableDef.FLOW_BUSINESS.TYPE.eq(0));
        queryWrapper.where(FlowBusinessTableDef.FLOW_BUSINESS.INFLOW_DIRECTION.eq(0));
        queryWrapper.where(FlowBusinessTableDef.FLOW_BUSINESS.INFLOW_WAY.eq(0));
        queryWrapper.where(FlowBusinessTableDef.FLOW_BUSINESS.STATE.eq(1));
        queryWrapper.orderBy("create_time desc");
        //是否分页
        if(oFlowBusinessDto.getPage() != null && oFlowBusinessDto.getLimit() != null){
            Page<FlowBusiness> FlowBusinessPage = oFlowBusinessMapper.paginateWithRelations(oFlowBusinessDto.getPage(),oFlowBusinessDto.getLimit(),queryWrapper);
            oFlowBusinessList = FlowBusinessPage.getRecords();
            total = FlowBusinessPage.getTotalRow();
        }else{
            oFlowBusinessList = oFlowBusinessMapper.selectListWithRelationsByQuery(queryWrapper);
            total = oFlowBusinessList.size();
        }
        //PoToDto
        List<FlowBusinessDto> oFlowBusinessDtoList = (List<FlowBusinessDto>) PoToDTO.poToDtoList(oFlowBusinessList,new FlowBusinessDto());
        return Result.ok(oFlowBusinessDtoList,total);
    }

}
